﻿/*
 * 文件名称：
 * 程序员：wangyun
 * 创建时间：2008/05/30
 * 
 * 功能说明：表单添加更新页面
 * 
 * 修改人员：
 * 
 * 修改日期：
 * 
 * 修改记录：
 */

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Whir.Common.WebHelper;

public partial class Whir_System_Module_CommonModel_ModelForm : System.Web.UI.Page
{
    protected string Action = "";
    protected int ModelFormID = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        RoleHelper.IsLogin();    //登录判断

        Action = NetRequest.GetQueryString("Action");
        ModelFormID = NetRequest.GetQueryInt("ModelFormID");

        if (!IsPostBack)
        {
            switch (Action)
            {
                case "Add":
                    BindModelsList();
                    BindFieldsList();
                    btnSubmit.Text = "添加";
                    break;
                case "Modify":
                    BindModelsList();
                    SetFieldsValue();
                    btnSubmit.Text = "修改";
                    break;
                case "Delete":
                    BindModelsList();
                    SetFieldsValue();
                    btnSubmit.Text = "删除";
                    break;
                default:
                    break;
            }
        }
    }

    /// <summary>
    /// 设置字段值
    /// </summary>
    private void SetFieldsValue()
    {
        Whir.BLL.Whir_ModelForm bll = new Whir.BLL.Whir_ModelForm();
        Whir.Model.Whir_ModelForm model = new Whir.Model.Whir_ModelForm();
        model = bll.GetModel(ModelFormID);

        //功能模块选择禁用
        ddlModelName.Enabled = false;
        ddlModelName.SelectedValue = model.modelID.ToString();

        //初始化字段列表、显示隐藏列表、搜索条件列表
        BindFieldsList();

        string fieldIDs = model.fieldIDs;
        string[] temp = fieldIDs.Split(',');

        for (int i = 0; i < chkFieldIDs.Items.Count; i++)
        {
            for (int j = 0; j < temp.Length; j++)
            {
                if (temp[j].Split('|')[0] == chkFieldIDs.Items[i].Value)
                {
                    chkFieldIDs.Items[i].Selected = true;

                    if (temp[j].Split('|')[1] == "true")
                        chkIsHide.Items[i].Selected = true;
                    else
                        chkIsHide.Items[i].Selected = false;
                    break;
                }
                else
                {
                    chkIsHide.Items[i].Selected = false;
                }
            }
        }

        temp = model.formsearch.Split(',');
        for (int i = 0; i < chkSearch.Items.Count; i++)
        {
            for (int j = 0; j < temp.Length; j++)
            {
                if (temp[j] == chkSearch.Items[i].Value)
                {
                    chkSearch.Items[i].Selected = true;
                    break;
                }
            }
        }

        //string formsearch = model.formsearch;
        //for (int i = 0; i < rbSearch.Items.Count; i++)
        //{
        //    if (formsearch == rbSearch.Items[i].Value)
        //    {
        //        rbSearch.Items[i].Selected = true;
        //        break;
        //    }
        //}

        //初始化其它控件
        txtFormName.Text = model.formName;
        txtFormDesc.Text = model.formDesc;
        chkTimeSpanSearch.Checked = model.timespanSearch;
    }

    /// <summary>
    /// 绑定功能模块列表
    /// </summary>
    private void BindModelsList()
    {
        Whir.BLL.Whir_Model bll = new Whir.BLL.Whir_Model();
        ddlModelName.DataSource = bll.GetList();
        ddlModelName.DataTextField = "modelName";
        ddlModelName.DataValueField = "modelID";
        ddlModelName.DataBind();
    }

    /// <summary>
    /// 绑定字段列表
    /// </summary>
    private void BindFieldsList()
    {
        int modelID = Convert.ToInt32(ddlModelName.SelectedValue);
        if (modelID != 0)
        {
            Whir.BLL.Whir_Field fieldBll = new Whir.BLL.Whir_Field();
            DataView dv = fieldBll.GetList(" modelID =" + modelID).Tables[0].DefaultView;
            dv.RowFilter = "IsUse = 1";
            //绑定字段列表
            chkFieldIDs.DataSource = dv;
            chkFieldIDs.DataTextField = "alias";
            chkFieldIDs.DataValueField = "fieldID";
            chkFieldIDs.DataBind();

            //绑定显示和隐藏列表
            chkIsHide.DataSource = dv;
            chkIsHide.DataTextField = "alias";
            chkIsHide.DataValueField = "fieldID";
            chkIsHide.DataBind();

            //绑定搜索列表
            chkSearch.DataSource = dv;
            chkSearch.DataTextField = "alias";
            chkSearch.DataValueField = "fieldID";
            chkSearch.DataBind();

            foreach (ListItem item in chkFieldIDs.Items)
            {
                if (GetFieldName(int.Parse(item.Value)) == "typeid" || GetFieldName(int.Parse(item.Value)) == "createdate")
                {
                    item.Selected = true;
                    item.Text += "&nbsp;(*必选)";
                    item.Enabled = false;
                }
            }

            foreach (ListItem item in chkIsHide.Items)
            {
                item.Text = "显示";
                item.Selected = true;
                if (GetFieldName(int.Parse(item.Value)) == "createdate")
                {
                    item.Selected = true;
                    item.Text += "&nbsp;(*必选)";
                    item.Enabled = false;
                }
            }

            foreach (ListItem item in chkSearch.Items)
            {
                item.Text = "搜索";

                if (GetFieldName(int.Parse(item.Value)) == "createdate")
                {
                    item.Selected = true;
                    item.Text += "&nbsp;(*必选)";
                    item.Enabled = false;
                }
            }
        }
    }

    protected string GetFieldName(int fieldID)
    {
        Whir.BLL.Whir_Field bll = new Whir.BLL.Whir_Field();
        return bll.GetModel(fieldID).fieldName.ToLower();
    }

    /// <summary>
    /// 获取选中的字段集合
    /// </summary>
    /// <returns>返回选中的字段集合</returns>
    private string GetSelectFields()
    {
        string strTemp = "";
        for (int i = 0; i < chkFieldIDs.Items.Count; i++)
        {
            if (chkFieldIDs.Items[i].Selected)
            {
                strTemp += chkFieldIDs.Items[i].Value + "|";
                if (chkIsHide.Items[i].Selected)
                    strTemp += "true" + ",";
                else
                    strTemp += "false" + ",";
            }
        }
        strTemp = strTemp.TrimEnd(',');
        return strTemp;
    }

    private string GetSelectSearchFields()
    {
        string strTemp = "";
        //strTemp = rbSearch.SelectedValue;
        //return string.IsNullOrEmpty(strTemp) ? "" : strTemp;

        for (int i = 0; i < chkSearch.Items.Count; i++)
        {
            if (chkSearch.Items[i].Selected)
            {
                strTemp += chkSearch.Items[i].Value + ",";
            }
        }
        strTemp = strTemp.TrimEnd(',');
        return strTemp;
    }

    protected void ddlModelName_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindFieldsList();
    }

    /// <summary>
    /// 执行添加或更新操作
    /// </summary>
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (GetSelectFields() == "")
        {
            JsHelper.Alert(Page, "字段选择不能为空");
            return;
        }

        Whir.BLL.Whir_ModelForm bll = new Whir.BLL.Whir_ModelForm();
        Whir.Model.Whir_ModelForm model;
        switch (Action)
        {
            case "Add"://添加
                model = new Whir.Model.Whir_ModelForm();

                model.modelID = Convert.ToInt32(ddlModelName.SelectedValue);
                model.formName = txtFormName.Text.Trim();
                model.formDesc = txtFormDesc.Text.Trim();
                model.fieldIDs = GetSelectFields();
                model.formContent = "";
                model.formsearch = GetSelectSearchFields();
                model.createUserID = 0;
                model.updateUserID = 0;
                model.timespanSearch = chkTimeSpanSearch.Checked;
                bll.Add(model);

                Response.Redirect("ModelFormManage.aspx?ModelID=" + model.modelID);
                break;
            case "Modify"://修改
                model = bll.GetModel(ModelFormID);

                model.modelID = Convert.ToInt32(ddlModelName.SelectedValue);
                model.formName = txtFormName.Text.Trim();
                model.formDesc = txtFormDesc.Text.Trim();
                model.fieldIDs = GetSelectFields();
                model.formContent = "";
                model.formsearch = GetSelectSearchFields();
                model.createUserID = 0;
                model.updateUserID = 0;
                model.timespanSearch = chkTimeSpanSearch.Checked;
                bll.Update(model);

                Response.Redirect("ModelFormManage.aspx?ModelID=" + model.modelID);
                break;
            case "Delete"://删除
                if (ContentManage.ModelFormInColumnCheck(ModelFormID))
                    JsHelper.Alert(Page, "该表单已经在栏目中绑定，请先删除栏目与表单的关联！");
                else
                {
                    bll.Delete(ModelFormID);
                    Response.Redirect("../Contents/ModelManage.aspx");
                }
                break;
        }
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("../Contents/ModelManage.aspx");
    }
}
